package com.sp.outputstream_;

import java.io.*;

/**
 * 二进制文件的复制
 */
public class BufferedCopy02_ {
    public static void main(String[] args) {
//        String srcFilePath = "d:\\development\\code\\IO_Test\\news\\11.jpg";
//        String destFilePath = "d:\\development\\code\\IO_Test\\news\\11dest.jpg";
//        String srcFilePath = "d:\\development\\code\\IO_Test\\news\\1.mp3";
//        String destFilePath = "d:\\development\\code\\IO_Test\\news\\2.mp3";
        //也可以操作文本文件
        String srcFilePath = "d:\\development\\code\\IO_Test\\news\\story.txt";
        String destFilePath = "d:\\development\\code\\IO_Test\\news\\story1.txt";

        //创建BufferedOutputStream对象和 BufferedInputStream对象
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));

            //循环的读取文件, 并写入到destFilePath
            byte[] buff = new byte[1024];
            int readLen = 0;
            while ((readLen = bufferedInputStream.read(buff)) != -1) {
                bufferedOutputStream.write(buff, 0, readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流, 关闭外层的处理流, 底层会自动去关闭节点流
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
                if (bufferedOutputStream != null){
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
